﻿using DimensionsHelper.LanguageService.Protocol;

namespace DimensionsHelper.LanguageService.Syntax;

public interface ITextRange
{
    /// <summary>
    ///     文本区间所在的文件URI
    /// </summary>
    DocumentUri Uri { get; }


    /// <summary>
    ///     获取文本区间的文本区间
    /// </summary>
    TextSpan Span { get; }


    /// <summary>
    ///     获取区间的开始位置
    /// </summary>
    int Start { get; }


    /// <summary>
    ///     获取区间的起始行
    /// </summary>
    int StartLine { get; }


    /// <summary>
    ///     获取区间的结束位置
    /// </summary>
    int End { get; }


    /// <summary>
    ///     获取区间的结束行
    /// </summary>
    int EndLine { get; }


    /// <summary>
    ///     获取区间的宽度
    /// </summary>
    int Width { get; }


    /// <summary>
    ///     获取文本区间对应的字符串
    /// </summary>
    string Text { get; }


    /// <summary>
    ///     如果区间是空区间，返回<see langword="true" />
    /// </summary>
    bool IsEmpty { get; }


    /// <summary>
    ///     如果区间是注释，返回<see langword="true" />
    /// </summary>
    bool IsComment { get; }


    /// <summary>
    ///     对区间的<see cref="Start" />进行偏移，
    ///     行号和列号不变。
    /// </summary>
    /// <param name="offset">文本偏移量</param>
    void Offset(int offset);
}


public static partial class SyntaxExtensions
{
    public static int GetLines(this ITextRange range)
    {
        int lines = range.EndLine - range.StartLine + 1;
        return Math.Max(lines, 1);
    }


    public static SyntaxLocation GetLocation(this ITextRange textRange)
    {
        return SyntaxLocation.Create(textRange.Uri,
            textRange.Start, textRange.Width,
            textRange is SyntaxCommentLine or SyntaxComment);
    }
}